import type { Bullets } from "./bullets";
import { Enemys } from "./enemy";
import { MyPlane } from "./myPlane";
import {crashTest} from "./common";
export class GameEngine {
  public el: HTMLDivElement;
  public level: number = 0;
  public bullets: Array<Bullets> = []; //所有子弹的对象
  public enemys: Array<Enemys> = []; //所有敌人飞机的对象
  static instanceObj: GameEngine;
  constructor() {
    this.el = document.querySelector("#main_body") as HTMLDivElement;
    this.init();
  }
  static instance() {
    if (GameEngine.instanceObj == null) {
      GameEngine.instanceObj = new GameEngine();
    }
    return GameEngine.instanceObj;
  }

  init() {
    this.el.addEventListener("click", (e: Event) => {
      let target: HTMLElement = e.target as HTMLElement;
      if (target.nodeName != "LI") return;
      this.level = Number(target.getAttribute("value")); //获得有点的难度值
      //开始游戏
      this.start();
    });
  }
  //开始游戏
  start() {
    this.el.innerHTML = "";
    this.loading(() => {
      this.el.innerHTML = "";
      MyPlane.instance();
      this.controller();
      MyPlane.instance().fire();//战机开火
      this.buildEnemys();//生成敌机
      this.loopTest();
    });
  }
  //加载进度条
  loading(callback: Function) {
    let logo: HTMLDivElement = document.createElement("div");
    logo.classList.add("logo");
    this.el.appendChild(logo);

    let loading: HTMLDivElement = document.createElement("div");
    loading.className = "loading";
    this.el.appendChild(loading);

    let lodingIndex = 0;
    let timer = setInterval(() => {
      lodingIndex++;
      loading.style.backgroundImage = `url(/src/assets/images/loading${
        (lodingIndex % 3) + 1
      }.png)`;

      if (lodingIndex >= 5) {
        clearInterval(timer);
        if (callback && typeof callback == "function") {
          callback();
        }
      }
    }, 100);
  }
  //控制战机左右移动
  controller() {
    window.addEventListener("keydown", (e: KeyboardEvent) => {
      if (e.keyCode == 37) {
        //新的位置=当前位置+步长
        let mX = MyPlane.instance().el.offsetLeft - 20;
        if (mX <= 0) {
          mX = 0;
        }
        MyPlane.instance().el.style.left = mX + "px";
      }
      if (e.keyCode == 39) {
        let mX = MyPlane.instance().el.offsetLeft + 20;
        let maxWidth =
          GameEngine.instance().el.offsetWidth -
          MyPlane.instance().el.offsetWidth;
        if (mX > maxWidth) {
          mX = maxWidth;
        }
        MyPlane.instance().el.style.left = mX + "px";
      }
    });
  }
  buildEnemys() {
    setInterval(() => {
      new Enemys();
    },2000);
  }

  loopTest(){

     let timer= setInterval(()=>{
          for (const bb in this.bullets) {
            for (const ee in this.enemys) {
                 let isFlag= crashTest(this.bullets[bb].el,this.enemys[ee].el);
                 if (isFlag) {
                  this.enemys[ee].hurt();
                  this.bullets[bb].remove();
                  break;
                 }
              }
            }

            //敌人飞机与战机的碰撞
            for (const ee in this.enemys) {
              let isFlag= crashTest(MyPlane.instance().el,this.enemys[ee].el); 
              if (isFlag) {
                clearInterval(timer);
                alert("GAME OVER!"); 
                window.location.reload();
                break;
              }
            }
      })
      

  }
}
